package com.ruoyi.wetest.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.wetest.domain.WeLoadMasterDataCollectStartReq;
import com.ruoyi.wetest.domain.WeLoadMasterDataCollectStartResp;
import com.ruoyi.wetest.domain.WeLoadMasterDataCollectStopReq;
import com.ruoyi.wetest.domain.WeTestBaseReq;
import com.ruoyi.wetest.manager.WetestManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Allen
 * @description TODO
 * @Date 2024/4/26 20:06
 */
@RestController
@RequestMapping("/loadmaster")
public class WeTestLoadMasterController extends BaseController {

    @Autowired
    private WetestManager wetestManager;

    /**
     * 开始压测
     * WetestAPI: start test success! dcAddr:101.91.19.182:9126,instanceId:1,testId:7e2334efa5cf5e91bbf8339ab4b39cbf,token:6d93085c7d84687aca14e9ad3630c946,gapsHost:http://101.91.19.182:9126
     * WetestAPI: start test success! dcAddr:101.91.19.182:9126,instanceId:0,testId:7e2334efa5cf5e91bbf8339ab4b39cbf,token:6d93085c7d84687aca14e9ad3630c946,gapsHost:http://101.91.19.182:9126
     * <p>
     * url:https://api.paas.wetest.tencent.com/loadmaster/data-collect/start-test?nonce=0&projectid=neo-3646&secret_id=b0df571783f34ee693291e07dd91b1f4&t=1714447895&zoneid=0&sign=T9VjLvEYdnLrZAitCpe07OQh%2FIjEcq3oqyECKqqQpkk%3D,
     * resp:{"ret":0,"msg":"","token":"6d93085c7d84687aca14e9ad3630c946","testId":"68de5235f4ec43328d4ff56c7f869547","dcAddr":"101.91.19.182:9126","instanceId":0}
     */
    @GetMapping("/data-collect/start-test")
    public AjaxResult dataCollectStart(WeLoadMasterDataCollectStartReq req) {
       /* String projectid = req.getProjectid();
        //{"ret":0,"msg":"","token":"6d93085c7d84687aca14e9ad3630c946","testId":"f1116bb9d604e0d6170bad4dbce641b9","dcAddr":"101.91.19.182:9126","instanceId":0}
        result.put("token", "6d93085c7d84687aca14e9ad3630c946");
        result.put("testId", "123456");
        result.put("dcAddr", "172.18.4.80:8080");
        result.put("instanceId", "0");*/
        WeLoadMasterDataCollectStartResp start = wetestManager.start(req);
        AjaxResult result = success();
        result.put("token", start.getToken());
        result.put("testId", start.getTestId());
        result.put("dcAddr", start.getDcAddr());
        result.put("instanceId", start.getInstanceId());
        return AjaxResult.toWetest(result);
    }

    /**
     * 停止压测
     * url:http://101.91.19.182:9126/gapsapi/data_collect_v1/statistics_info?instanceid=0&nonce=0&secret_id=b0df571783f34ee693291e07dd91b1f4&t=1714447916&testid=68de5235f4ec43328d4ff56c7f869547&token=6d93085c7d84687aca14e9ad3630c946&sign=4Q9MiAr11BLHdJYwsdLdDRVXQ7t2mPct4hVMxLAXoyw%3D
     * resp:{"ret":0,"msg":"ok"}
     */
    @GetMapping("/data-collect/stop-test")
    public AjaxResult dataCollectStop(WeLoadMasterDataCollectStopReq req) {
        wetestManager.stop(req.getProjectid());
        AjaxResult result = success();
        return AjaxResult.toWetest(result);
    }

    @GetMapping("/data-collect/list")
    public AjaxResult dataCollectList() {
        List<String> stressList = wetestManager.getStressList();
        AjaxResult result = success(stressList);
        return AjaxResult.toWetest(result);
    }

    @GetMapping("/data-collect/snapshot")
    public AjaxResult dataCollectMetricSnapshot(String testid) {
        AjaxResult result = success(wetestManager.getMetricSnapshot(testid));
        return AjaxResult.toWetest(result);
    }


}
